package com.test;

/***
 * @ClassName: MailLambda
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 9:53 2022/2/24
 * @version : V1.0
 */
public class MailLambda {
    private Thread dog = null;
    private Thread cat = null;

    public MailLambda() {

        if (dog == null)
            dog = new DogThread();
        dog.setName("狗");
        if (!dog.isAlive())
            dog.start();

        if (cat == null)
            cat = new CatThread();
        cat.setName("猫");
        if (!cat.isAlive())
            cat.start();
    }

    public static void main(String[] args) throws InterruptedException {
        com.test.MailTest mailTest = new com.test.MailTest();
        Thread.sleep(13000);
        mailTest.stop();
    }

    public void stop() {
        if (dog != null)
            dog.interrupt();
        if (cat != null)
            cat.interrupt();
    }

    private class DogThread extends Thread {
        @Override
        public void run() {
            try {
                //判断线程的终止标志
                while (!isInterrupted()) {
                    Thread.sleep(12 * 1000);
                    System.out.println("狗休息了十二秒");
                }
                System.out.println(isInterrupted());
            } catch (InterruptedException e) {
                System.out.println("狗睡眠进入异常");
                e.printStackTrace();
            }
        }
    }

    private class CatThread extends Thread {
        @Override
        public void run() {
            try {
                //判断线程的终止标志
                while (!isInterrupted()) {
                    Thread.sleep(5 * 1000);
                    System.out.println("猫休息了五秒");
                }
                System.out.println(isInterrupted());
            } catch (InterruptedException e) {
                System.out.println("猫睡眠进入异常");
                e.printStackTrace();
            }
        }
    }
}

